package com.qf.dao.impl;

import com.qf.dao.OrderDetailDao;
import com.qf.entity.Address;
import com.qf.entity.Goods;
import com.qf.entity.Order;
import com.qf.entity.OrderDetail;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 10202
 * @version V1.0
 * @Project xiaomi
 * @Package com.qf.dao.impl
 * @Date 2023/4/15 14:40
 */
public class OrderDetailDaoImpl implements OrderDetailDao {
    @Override
    public void addOrderDetail(OrderDetail orderDetail) {
        QueryRunner qr=new QueryRunner();
        Connection conn = DruidUtils.getConnection();
        Object[] params={orderDetail.getId(),orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            qr.update(conn,"insert into tb_orderdetail values (?,?,?,?,?)",params);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }finally {
            DruidUtils.closeAll(null,null,conn);
        }
    }

    @Override
    public OrderDetail queryOrderDetail(String oid) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            String sql="select g.*," +
                    "d.id as did, d.num,d.money as dmoney," +
                    "o.uid,o.money as omoney,o.status,o.time,o.aid," +
                    "a.detail,a.name as aname,a.phone,a.level as alevel " +
                    " FROM tb_goods as g inner join tb_orderdetail as d ON g.id=d.pid " +
                    "INNER JOIN tb_order as o ON o.id=d.oid  " +
                    "INNER JOIN tb_address as a ON a.id=o.aid " +
                    "where d.oid=?";
            return qr.query(sql, new ResultSetHandler<OrderDetail>() {
                @Override
                public OrderDetail handle(ResultSet resultSet) throws SQLException {
                    OrderDetail orderDetail=null;
                    List<Goods> goodsList=new ArrayList<>();
                    if(resultSet.next()){
                        int gid = resultSet.getInt("id");
                        String gname = resultSet.getString("name");
                        Date pubdate = resultSet.getTimestamp("pubdate");
                        String picture = resultSet.getString("picture");
                        BigDecimal price = resultSet.getBigDecimal("price");
                        int star = resultSet.getInt("star");
                        String intro = resultSet.getString("intro");
                        int typeid = resultSet.getInt("typeid");
                        int did = resultSet.getInt("did");
                        int num = resultSet.getInt("num");
                        BigDecimal dmoney = resultSet.getBigDecimal("dmoney");
                        int uid = resultSet.getInt("uid");
                        BigDecimal omoney = resultSet.getBigDecimal("omoney");
                        String status = resultSet.getString("status");
                        Date time = resultSet.getTimestamp("time");
                        int aid = resultSet.getInt("aid");
                        String detail = resultSet.getString("detail");
                        String aname = resultSet.getString("aname");
                        String phone = resultSet.getString("phone");
                        int alevel = resultSet.getInt("alevel");
                        Goods goods=new Goods(gid,gname,pubdate,picture,price,star,intro,typeid);
                        goodsList.add(goods);
                        orderDetail=new OrderDetail(did,oid,gid,num,dmoney,new Order(oid,uid,status,omoney,time,aid),new Address(aid,detail,aname,phone,uid,alevel),null);
                    }
                    while (resultSet.next()){
                        int gid = resultSet.getInt("id");
                        String gname = resultSet.getString("name");
                        Date pubdate = resultSet.getTimestamp("pubdate");
                        String picture = resultSet.getString("picture");
                        BigDecimal price = resultSet.getBigDecimal("price");
                        int star = resultSet.getInt("star");
                        String intro = resultSet.getString("intro");
                        int typeid = resultSet.getInt("typeid");
                        Goods goods=new Goods(gid,gname,pubdate,picture,price,star,intro,typeid);
                        goodsList.add(goods);
                    }
                    orderDetail.setList(goodsList);
                    return orderDetail;
                }
            },oid);
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }
}
